package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)
var wg sync.WaitGroup

func cpuInfo(ctx context.Context)  {
	defer wg.Done()
	for {
		select {
		case <- ctx.Done():
			fmt.Println("cpu监控退出")
			return
		default:
			time.Sleep(time.Second)
			fmt.Println("cpu监控信息获取成功")
		}
	}
}

func cpuInfo1(ctx context.Context)  {
	defer wg.Done()
	for {
		select {
		case <- ctx.Done():
			fmt.Println("cpu监控1退出")
			return
		default:
			time.Sleep(time.Second)
			fmt.Println("cpu监控信息1获取成功")
		}
	}
}

func main() {
	wg.Add(2)
	//ctx, cancel := context.WithCancel(context.Background())
	ctx, _ := context.WithTimeout(context.Background(), time.Second * 3)
	go cpuInfo(ctx)
	go cpuInfo1(ctx)
	// 模拟读取
	time.Sleep(time.Second * 6)
	//cancel()
	wg.Wait()
	fmt.Println("信息读取成功")

}
